package com.szl.code;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.IOException;

import static java.lang.Thread.sleep;

public class Attach {
    public static void main(String[] args) throws IOException, AttachNotSupportedException {
        //  表示目标进程的PID
        VirtualMachine virtualMachine = VirtualMachine.attach("18844");
        // 指定Java Agent的jar包路径
        try {
            virtualMachine.loadAgent("F:\\javacode\\InstrumentationCode\\JavaAgent\\target\\JavaAgent-1.0-SNAPSHOT-jar-with-dependencies.jar", "sizengli");
        } catch (AgentLoadException e) {
            e.printStackTrace();
        } catch (AgentInitializationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            virtualMachine.detach();
        }
    }
}
